<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadFileController extends Controller
{
    public function upload_image(Request $request)
    {
        if(!is_object(auth()->user()))
        {
            $data['success'] = $data['code'] = 0;
            $data['message'] = '登录过期！';
            $data['fileName'] =  '';//ckeditor
            $data['url'] = '';
            return json_encode($data);
        }
        if ($request->isMethod('POST')) {
            $data_dir = date('Ym',time()).'/';
            $name = array_keys($request->file());
            $file = $request->file($name[0]);

            if ($file->isValid()){
                //源文件名
                $originalName = $file->getClientOriginalName();
                //扩展名
                $ext = $file->getClientOriginalExtension();
                //文件类型
                $type = $file->getClientMimeType();
                //临时文件的绝对路径
                $realPath = $file->getRealPath();
                //文件大小
                $sizes = $file->getSize();
                //准备存储的自定义文件名
                $filename = date('YmdHis') . '_' . uniqid(rand(100,999)) . '.' . $ext;//uniqid();

                //判断文件是否超过限制的大小  默认字节单位  5242880=5M  1048576 ≈ 1M
                if ($sizes > 5*1024*1024){
                    $data['success'] = $data['code'] = 1;
                    $data['message'] = '上传的文件过大，请上传小于5M的文件！';
                    $data['fileName'] =  '';//ckeditor
                    $data['url'] = '';
                    return json_encode($data);
                }

                if (in_array($ext,["jpg", "jpeg", "gif", "png", "bmp", "webp"])){
                    //存储方式一：（返回完整路径）
                    //return $file -> move(public_path().'/uploads/tttt',$filename);

                    //存储方式二：（依赖disk配置属性，配置文件在config/filesystems.php。 返回布尔值）
                    $bools = Storage::disk('uploads')->put('images/' . $data_dir . $filename,file_get_contents($realPath));
                    if ($bools){
                        $data['success'] = $data['code'] = 0;
                        $data['message'] = 'ok';
                        $data['url'] = '/uploads/images/' . $data_dir . $filename;//ckeditor
//                        return $data['url'];

                        $data['fileName'] =  $filename;//ckeditor
                        $data['uploaded'] = 1;//ckeditor
                        return json_encode($data);
                    }else{
                        $data['success'] = $data['code'] = 2;
                        $data['message'] = '系统文件转移错误，请稍后重新上传！';
                        $data['fileName'] =  '';//ckeditor
                        $data['url'] = '';
                        return json_encode($data);
                    }
                }else{
                    $data['success'] = $data['code'] = 3;
                    $data['message'] = '格式不对！';
                    $data['fileName'] =  '';//ckeditor
                    $data['url'] = '';
                    return json_encode($data);
                }
            }
        }

    }
}
